﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lyon.Util.Helper
{
    public static class RandomHelper
    {
        /// <summary>
        /// 返回一个随机bool值
        /// </summary>
        /// <param name="random"></param>
        /// <returns></returns>
        public static bool NextBool(this Random random)
        {
            return random.NextDouble() > 0.5;
        }

        public static T NextEnum<T>(this Random random) where T : struct
        {
            Type type = typeof(T);
            if (type.IsEnum == false) throw new InvalidOperationException();

            var array = Enum.GetValues(type);
            var index = random.Next(array.GetLowerBound(0), array.GetUpperBound(0) + 1);
            return (T)array.GetValue(index);
        }
    }
}
